package com.daikin.ktmustwrite.viewmodel.request

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.daikin.ktmustwrite.app.network.apiService
import com.daikin.ktmustwrite.app.network.stateCallback.ListDataUIState
import com.daikin.ktmustwrite.data.model.bean.AriticleResponse
import com.daikin.ktmustwrite.data.model.bean.ClassifyResponse
import com.daikin.ktmustwrite.data.repository.request.HttpRequestCoroutine
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

class RequestProjectViewModel : BaseViewModel() {
    //页码
    var pageNo = 1
    var titleData: MutableLiveData<ResultState<ArrayList<ClassifyResponse>>> = MutableLiveData()
    var projectDataState: MutableLiveData<ListDataUIState<AriticleResponse>> = MutableLiveData()

    /** */

    fun getProjectTitleData() {
        Log.e("lhtest", "getProjectTitleData: ", )
        request({ apiService.getProjecTitle() }, titleData)
    }

    fun getProjectData(isRefresh: Boolean, cid: Int, isNew: Boolean = false) {
        if (isRefresh) {
            pageNo = if (isNew) 0 else 1
        }
        request({ HttpRequestCoroutine.getProjectData(pageNo, cid, isNew) }, {
            //请求成功
            pageNo++
            val listDataUiState =
                ListDataUIState(
                    isSuccess = true,
                    isRefresh = isRefresh,
                    isEmpty = it.isEmpty(),
                    hasMore = it.hasMore(),
                    isFirstEmpty = isRefresh && it.isEmpty(),
                    listData = it.datas
                )
            projectDataState.value = listDataUiState
        },
            {
                //请求失败
                val listDataUiState =
                    ListDataUIState(
                        isSuccess = false,
                        errMessage = it.errorMsg,
                        isRefresh = isRefresh,
                        listData = arrayListOf<AriticleResponse>()
                    )
                projectDataState.value = listDataUiState
            })
    }

}